Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmHistory
    Public GV_ISEDIT As Boolean = False
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        GV_ISEDIT = False

        Panel2.Enabled = True
        btnEdit.Visible = False
        btnDel.Visible = False
        btnAdd.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True
        ClearText()
    End Sub
    Private Sub ClearText()
        cbxStatus.SelectedIndex = -1
        cbxHistory.SelectedIndex = -1
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If GV_SELECTED_HISTORYID <> "" Then
            GV_ISEDIT = True
            Panel2.Enabled = True
            btnEdit.Visible = False
            btnDel.Visible = False
            btnAdd.Visible = False
            btnSave.Visible = True
            btnCancel.Visible = True
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SavePatientHistory()
        Panel2.Enabled = False
        Close()

        If GV_SELECTED_DATAENTRY_MODE = "PRESENTATION" Then
            FrmPresentDataEntry.Close()
            FrmPresentDataEntry.Show()
            FrmPresentDataEntry.Focus()

        Else
            FrmDiagnosis.Close()
            FrmDiagnosis.Show()
            FrmDiagnosis.Focus()
        End If

    End Sub
    Private Sub SavePatientHistory()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spInsertHistory", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@History_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
        myCommand.Parameters.Add("@FamilyHistory", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Menstrual", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)

        If GV_ISEDIT = True Then
            myCommand.Parameters("@History_ID").Value = GV_SELECTED_HISTORYID
        Else
            myCommand.Parameters("@History_ID").Value = "0"
        End If
        myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
        myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@FamilyHistory").Value = Trim(cbxHistory.Text)
        myCommand.Parameters("@Menstrual").Value = Trim(cbxStatus.Text)
        myCommand.Parameters("@EnteredBy").Value = GV_USER

        myCommand.ExecuteNonQuery()



        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub
    Private Sub LoadFamilyHistory()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 2", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxHistory.DataSource = list
            Me.cbxHistory.DisplayMember = "code_desc"
            Me.cbxHistory.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadMenstrualStatus()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 1", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxStatus.DataSource = list
            Me.cbxStatus.DisplayMember = "code_desc"
            Me.cbxStatus.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If GV_SELECTED_HISTORYID <> "" Then
            Dim response As Integer

            response = MsgBox("Are you sure want to delete this record?", MsgBoxStyle.YesNo, "Delete")
            If response = 7 Then
                Exit Sub
            End If

            Dim myCommand As SqlCommand
            Dim SqlConn As New SqlConnection()
            SqlConn = New SqlConnection(GV_CONNETION)
            myCommand = New SqlCommand("spDelHistory", SqlConn)
            myCommand.CommandType = CommandType.StoredProcedure
            SqlConn.Open()

            myCommand.Parameters.Add("@History_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
            myCommand.Parameters("@History_ID").Value = GV_SELECTED_HISTORYID
            myCommand.Parameters("@EnteredBy").Value = GV_USER
            myCommand.ExecuteNonQuery()

            myCommand.Cancel()
            SqlConn.Close()
            myCommand = Nothing
            SqlConn = Nothing
            Close()
            FrmDiagnosis.Close()
            FrmDiagnosis.Show()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If GV_ISEDIT = False Then ClearText() Else ListHistory(GV_SELECTED_HISTORYID)
        Panel2.Enabled = False
        btnAdd.Visible = False
        btnEdit.Visible = True
        btnDel.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False
    End Sub

    Private Sub FrmHistory_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Panel2.Enabled = False
        LoadFamilyHistory()
        LoadMenstrualStatus()
        If GV_SELECTED_HISTORYID <> "" Then
            ListHistory(GV_SELECTED_HISTORYID)
            If GV_FRMEVENT_ALLOW_EDIT = True Then btnEdit_Click(sender, e)
        End If
        If Approved() = True Then
            'btnAdd.Enabled = False
            'btnEdit.Enabled = False
            'btnDel.Enabled = False
            'Panel1.Enabled = False
            'Panel2.Enabled = False
            btnEdit.Enabled = False
            btnSave.Enabled = False
            BtnLock.Text = "Unlock"
            btnDel.Enabled = False
        End If
        Panel1.Enabled = GV_FRMEVENT_ALLOW_EDIT
    End Sub
    Function Approved() As Boolean
        Dim vStatus As String = ""
        Dim vLockBy As String = ""

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from history where history_id = '" & GV_SELECTED_HISTORYID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vStatus = IIf(IsDBNull(objReader("Status")), "", objReader("Status"))
                vLockBy = IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        If vLockBy <> "" Then
            Approved = True
        Else
            Approved = False
        End If
    End Function
    Private Sub ListHistory(ByVal vHistoryID)
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from history where history_id = '" & vHistoryID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                cbxHistory.Text = IIf(IsDBNull(objReader("FamilyHistory")), "", objReader("FamilyHistory"))
                cbxStatus.Text = IIf(IsDBNull(objReader("Menstrual")), "", objReader("Menstrual"))
                Lbl_EnBy.Text = "Entered By:" & vbCrLf & IIf(IsDBNull(objReader("EnteredBy")), "", objReader("EnteredBy"))
                Lbl_UpdBy.Text = "Updated By:" & vbCrLf & IIf(IsDBNull(objReader("UpdatedBy")), "", objReader("UpdatedBy"))
                Lbl_LockBy.Text = "Locked By:" & vbCrLf & IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))

            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub BtnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLock.Click
        If BtnLock.Text = "Lock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        If BtnLock.Text = "UnLock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("sp_LockUnlockHistory", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@History_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@LockFlag", SqlDbType.VarChar)

        myCommand.Parameters("@History_ID").Value = GV_SELECTED_HISTORYID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.Parameters("@LockFlag").Value = Trim(BtnLock.Text)
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing

        If BtnLock.Text = "Lock" Then
            BtnLock.Text = "UnLock"
            btnEdit.Enabled = False
            btnSave.Enabled = False
            btnDel.Enabled = False
            Lbl_LockBy.Text = "Locked By:" & vbCrLf & GV_USERNAME
        Else
            BtnLock.Text = "Lock"
            btnEdit.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnSave.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnDel.Enabled = GV_FRMEVENT_ALLOW_EDIT
            Lbl_LockBy.Text = "Locked By:"
        End If
    End Sub
End Class